//
//  RootViewController.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/10.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import Networking
import Alamofire
import PKHUD
import SwiftyJSON
import Toast_Swift

class RootViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavigation()
        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if MineInfoModel.share.isLogin {
            let requesturl = "\(BaseURL)\(getUserInfo)"
            Alamofire.request(requesturl, method: .get,
                              parameters: nil,
                              encoding: URLEncoding.default,
                              headers: ["token":MineInfoModel.share.token!]).responseJSON {
                                (response) in
                                if(response.error == nil){
                                    
                                    if let result = response.result.value as? NSDictionary
                                    {
                                        let json = JSON(result)
                                        if json["code"].int == 1{
                                            let dic = json["data"].dictionary
                                            var userModel = UserInfoModel(fromDic: dic!)
                                            if let dict = json["data"]["grade"].dictionary {
                                                userModel.initGrade(dic: dict)
                                            }
                                            
                                            let mineInfoModel = MineInfoModel.share
                                            mineInfoModel.userInfModel = userModel
                                            mineInfoModel.token = MineInfoModel.share.token!
                                            mineInfoModel.isLogin = true
                                            mineInfoModel.saveUserData(userinfo: userModel.toDictionary() as NSDictionary)
                                            mineInfoModel.loadUserData()
                                        }else if json["code"].int == 100{
                                            self.showToast(msg:json["message"].string!)
                                            let loginVC = LoginViewController(nibName: "\(LoginViewController.self)", bundle: nil)
                                            self.navigationController?.pushViewController(loginVC, animated: true)
                                        }else{
                                            self.showToast(msg:json["message"].string!)
                                        }
                                    }
                                }else{
                                    self.showToast(msg:"请求失败")
                                }
                                
            }
        }
    }
    
    func showToast(msg :String) -> Void {
        self.view.makeToast("一度蜜：" + msg, duration: 1.5, position: .center)
    }
    
    func setNavigation() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "home_left_menu"), style: .plain, target: self, action: #selector(leftMenuTouch))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "home_search"), style: .plain, target: self, action: #selector(rightMenuTouch))
    }
    
    @objc func leftMenuTouch() {
        LeftMenuView(tableTap: { index in
         
            if !MineInfoModel.share.isLogin {
                let loginVC = LoginViewController(nibName: "\(LoginViewController.self)", bundle: nil)
                self.navigationController?.pushViewController(loginVC, animated: true)
                //            CustomShowAlertView().show(title: "请先登录", tapClosure: {
                //
                //            })
                return
            }
            
            switch index {
//            case 0:
//                let yyDevicesVC = MyDevicesViewController(nibName: "\(MyDevicesViewController.self)", bundle: nil)
//                self.navigationController?.pushViewController(yyDevicesVC, animated: true)
            case 0:
                let yyCreationVC = MyCreationViewController(nibName: "\(MyCreationViewController.self)", bundle: nil)
                self.navigationController?.pushViewController(yyCreationVC, animated: true)
                
            case 1:
                let myOrderVC = NewOrderListViewController(nibName: "\(NewOrderListViewController.self)", bundle: nil)
                self.navigationController?.pushViewController(myOrderVC, animated: true)
            case 2:
                let mineMessageVC = MineMessageViewController(nibName: "\(MineMessageViewController.self)", bundle: nil)
                self.navigationController?.pushViewController(mineMessageVC, animated: true)
            case 3:
                let settingVC = SettingViewController(nibName: "\(SettingViewController.self)", bundle: nil)
                self.navigationController?.pushViewController(settingVC, animated: true)
            default:break
            }
        }) {
            switch MineInfoModel.share.isLogin {
            case true:
                let messageCirclePersonalCenterVC = PersonInfoViewController(nibName: "\(PersonInfoViewController.self)", bundle: nil)
                self.navigationController?.pushViewController(messageCirclePersonalCenterVC, animated: true)
            case false:
                let loginVC = LoginViewController(nibName: "\(LoginViewController.self)", bundle: nil)
                self.navigationController?.pushViewController(loginVC, animated: true)
            }
            }.show()
    }
    
    @objc func rightMenuTouch() {
        let searchVC = NewSearchViewController(nibName: "\(NewSearchViewController.self)", bundle: nil)
        self.navigationController?.pushViewController(searchVC, animated: true)
       
    }
    
}

extension UIViewController {
    func bannerJump(model: BannerModel) -> Void {
        if model.type == "1" {
            let vc = ConsultationDetailViewController(nibName: ("\(ConsultationDetailViewController.self)"), bundle: nil, type: HomeVCSelect.news)
            var temp: ArticleModel = ArticleModel()
            temp.id = model.id
            temp.title = model.title
            temp.coverImgPath = model.coverPath
            vc.model = temp
            if model.notiname != nil {
                vc.notiName = model.notiname!
            }
            self.navigationController?.pushViewController(vc, animated: true)
        }else if(model.type == "2"){
            let vc = VideoDetailViewController(nibName: ("\(VideoDetailViewController.self)"), bundle: nil, type: HomeVCSelect.video)
            var temp: VideoModel = VideoModel()
            temp.id = model.id
            vc.model = temp
            if model.notiname != nil {
                vc.notiName = model.notiname!
            }
            self.navigationController?.pushViewController(vc, animated: true)
        }else if(model.type == "3"){
            let vc = VideoDetailViewController(nibName: ("\(VideoDetailViewController.self)"), bundle: nil, type: HomeVCSelect.video)
            var temp: VideoModel = VideoModel()
            temp.id = model.id
            if model.notiname != nil {
                vc.notiName = model.notiname!
            }
            vc.model = temp
            self.navigationController?.pushViewController(vc, animated: true)
        }else if(model.type == "4"){
         let shoppingDetailVC = ShoppingDetailViewController(nibName: "\(ShoppingDetailViewController.self)", bundle: nil)
            var temp: GoodsModel = GoodsModel()
            temp.id = model.id
            shoppingDetailVC.commodityModel = temp
         self.navigationController?.pushViewController(shoppingDetailVC, animated: true)
        }else if(model.type == "5"){
            let web = WebViewController()
            web.webUrl = model.url!
            web.webTitle = model.title!
            self.navigationController?.pushViewController(web, animated: true)
        }
    }
}
